package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import domain.Comentario;

public class JDBCComentarioDAO implements IComentarioDAO{
	
	private Connection conn;
	
	public JDBCComentarioDAO(){
		conn = ConnectionManager.getInstance().checkOut();
	}
	
	public void anadirComentario(Comentario c){
		
		String sql = "INSERT INTO comentario ( manifestacionOID, user ,comentario ) VALUES (?, ?, ?) ";
        PreparedStatement stmt = null;

        try {
           
        	stmt=conn.prepareStatement(sql);
        	
        	stmt.setString(3, c.getTexto());
            stmt.setString(2, c.getOIDUsuario());
            stmt.setInt(1, c.getOIDManifestacion());
         
            
            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
  
	}
	
	 public void eliminarComentario(Integer comentarioOID) {
	        
		 String sql = "DELETE FROM comentario WHERE (comentarioOID = ?) ";
	       PreparedStatement stmt = null;

	        try {
	            stmt = conn.prepareStatement(sql);
	            
	            stmt.setLong(1, comentarioOID);
	            
	            stmt.executeUpdate();
	            
	        } catch (SQLException e) {
	            System.err.println("Message: " + e.getMessage());
	            System.err.println("SQLState: " + e.getSQLState());
	            System.err.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (stmt != null)
	                    stmt.close();
	            } catch (SQLException e) {
	            }
	        }
	    }
	
	 public Comentario select(Integer comentarioOID) {
	        
		 String sql = "SELECT * FROM comentario WHERE (comentarioOID = ? ) ";
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	        Comentario c = null;

	        try {
	            stmt = conn.prepareStatement(sql);
	            
	            stmt.setLong(1, comentarioOID);

	            result = stmt.executeQuery();

	            result.next();
	            c = new Comentario();
	            c.setOIDusuario(result.getString("user"));
	            c.setOIDmanifestacion(result.getInt("manifestacionOID"));
           	 	c.setTexto(result.getString("comentario"));
           	 	
           	  

	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return c;
	    }

	 
	 public List<Comentario> listarComentarios(Integer manifestacionOID){
			
		 PreparedStatement stmt = null;
	     ResultSet result = null;
	     List<Comentario> listaComentario = new LinkedList<Comentario>();
	        
	        try {
	        	String sql = "SELECT * FROM comentario WHERE ( manifestacionOID= ?)";
	            stmt = conn.prepareStatement(sql);
	            
	            stmt.setInt(1, manifestacionOID);

	            
	            
	                 result=stmt.executeQuery();
	                 while(result.next()){
	                	 Comentario c= new Comentario();
	                	 c.setOIDusuario(result.getString("user"));
	                	 c.setOIDmanifestacion(result.getInt("manifestacionOID"));
	                	 c.setTexto(result.getString("comentario"));
	                	 
	                	 listaComentario.add(c);
	                 }
	        
	            
	        }catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	        	ConnectionManager.getInstance().checkIn(conn);
	            try {
	                    if (result != null) {
	                            result.close();
	                    }
	                    if (stmt != null) {
	                            stmt.close();
	                    }
	            } catch (SQLException e) {
	            }
	        }
	        return listaComentario;
		
	 }
	 
	

}
